home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / c01lab4.zip / LRMRDR / LRM_CHAP.ZIP / CHAP06.DOC < prev    next >
Text File  |  1992-04-21  |  35KB  |  809 lines

  1. >                               6. Subprograms
  2.  
  3.  
  4. Subprograms  are  one  of the four forms of program unit, of which programs
  5. can be composed.  The other forms are packages,  task  units,  and  generic
  6. units.
  7.  
  8.  
  9. A  subprogram  is a program unit whose execution is invoked by a subprogram
  10. call.  There are two forms of  subprogram:  procedures  and  functions.   A
  11. procedure  call  is  a  statement;   a  function  call is an expression and
  12. returns a value.  The definition of a subprogram can be given in two parts:
  13. a subprogram declaration defining its calling conventions, and a subprogram
  14. body defining its execution.
  15.  
  16. References:  function 6.5, function call 6.4, generic unit 12,  package  7,
  17. procedure  6.1,  procedure  call  6.4, subprogram body 6.3, subprogram call
  18. 6.4, subprogram declaration 6.1, task unit 9
  19.  
  20. > 6.1  Subprogram Declarations
  21.  
  22.  
  23. A subprogram declaration declares a procedure or a function,  as  indicated
  24. by the initial reserved word.
  25.  
  26.  
  27.     subprogram_declaration ::= subprogram_specification;
  28.  
  29.     subprogram_specification ::=
  30.          procedure identifier [formal_part]
  31.        | function designator  [formal_part] return type_mark
  32.  
  33.     designator ::= identifier | operator_symbol
  34.  
  35.     operator_symbol ::= string_literal
  36.  
  37.     formal_part ::=
  38.        (parameter_specification {; parameter_specification})
  39.  
  40.     parameter_specification ::=
  41.        identifier_list : mode type_mark [:= expression]
  42.  
  43.     mode ::= [in] | in out | out
  44.  
  45.  
  46. The  specification  of  a procedure specifies its identifier and its formal
  47. parameters (if  any).   The  specification  of  a  function  specifies  its
  48. designator,  its formal parameters (if any) and the subtype of the returned
  49. value (the result subtype).  A designator that is  an  operator  symbol  is
  50. used  for  the  overloading  of  an  operator.   The sequence of characters
  51. represented by an operator symbol must be an operator belonging to  one  of
  52. the  six  classes  of  overloadable operators defined in section 4.5 (extra
  53. spaces are not allowed and the case of letters is not significant).
  54.  
  55.  
  56. A parameter specification with  several  identifiers  is  equivalent  to  a
  57. sequence  of  single parameter specifications, as explained in section 3.2.
  58. Each single parameter specification declares a  formal  parameter.   If  no
  59. mode  is  explicitly  given,  the  mode  in  is  assumed.   If  a parameter
  60. specification ends with  an  expression,  the  expression  is  the  default
  61. expression  of  the formal parameter.  A default expression is only allowed
  62. in a parameter specification if the  mode  is  in  (whether  this  mode  is
  63. indicated explicitly or implicitly).  The type of a default expression must
  64. be that of the corresponding formal parameter.
  65.  
  66.  
  67. The use of a name that denotes a formal parameter is not allowed in default
  68. expressions  of  a  formal  part  if  the specification of the parameter is
  69. itself given in this formal part.
  70.  
  71.  
  72. The elaboration of a subprogram declaration  elaborates  the  corresponding
  73. formal part.  The elaboration of a formal part has no other effect.
  74.  
  75.  
  76. Examples of subprogram declarations:
  77.  
  78.     procedure TRAVERSE_TREE;
  79.     procedure INCREMENT(X : in out INTEGER);
  80.     procedure RIGHT_INDENT(MARGIN : out LINE_SIZE);          --  see 3.5.4
  81.     procedure SWITCH(FROM, TO : in out LINK);                --  see 3.8.1
  82.  
  83.     function RANDOM return PROBABILITY;                      --  see 3.5.7
  84.  
  85.     function MIN_CELL(X : LINK) return CELL;                 --  see 3.8.1
  86.     function NEXT_FRAME(K : POSITIVE) return FRAME;          --  see 3.8
  87.     function DOT_PRODUCT(LEFT,RIGHT: VECTOR) return REAL;    --  see 3.6
  88.  
  89.     function "*"(LEFT,RIGHT : MATRIX) return MATRIX;         --  see 3.6
  90.  
  91.  
  92. Examples of in parameters with default expressions:
  93.  
  94.     procedure PRINT_HEADER(PAGES  : in NATURAL;
  95.                            HEADER : in LINE    :=  (1 .. LINE'LAST => ' ');  --  see 3.6
  96.                            CENTER : in BOOLEAN := TRUE);
  97.  
  98. Notes:
  99.  
  100.  
  101. The  evaluation  of  default  expressions  is  caused by certain subprogram
  102. calls, as described in section 6.4.2 (default expressions are not evaluated
  103. during the elaboration of the subprogram declaration).
  104.  
  105.  
  106. All subprograms can be called recursively and are reentrant.
  107.  
  108.  
  109. References:  declaration 3.1, elaboration 3.9, evaluation  4.5,  expression
  110. 4.4,  formal  parameter  6.2, function 6.5, identifier 2.3, identifier list
  111. 3.2, mode 6.2, name 4.1, elaboration has no other effect 3.9, operator 4.5,
  112. overloading 6.6 8.7, procedure 6, string literal 2.6, subprogram call  6.4,
  113. type mark 3.3.2
  114.  
  115. > 6.2  Formal Parameter Modes
  116.  
  117.  
  118. The value of an object is said to be read when this value is evaluated;  it
  119. is  also  said to be read when one of its subcomponents is read.  The value
  120. of a variable is said to be updated when an assignment is performed to  the
  121. variable,  and  also  (indirectly)  when  the  variable  is  used as actual
  122. parameter of a subprogram call or entry call  statement  that  updates  its
  123. value;   it  is  also  said  to be updated when one of its subcomponents is
  124. updated.
  125.  
  126.  
  127. A formal parameter of a subprogram has one of the three following modes:
  128.  
  129.  
  130.   in      The formal parameter  is a  constant  and  permits   only reading  of the value  of the
  131.           associated actual parameter.
  132.  
  133.  
  134.   in out  The formal parameter is a variable  and permits both reading and updating of the  value
  135.           of the associated actual parameter.
  136.  
  137.  
  138.   out     The formal parameter is a variable  and permits updating of the value of the associated
  139.           actual parameter.
  140.  
  141.           The value  of  a scalar parameter that is not updated by the call is undefined  upon return;
  142.           the same  holds for the  value  of a  scalar subcomponent,  other  than a  discriminant.
  143.           Reading the  bounds and discriminants of the  formal parameter  and  of its subcomponents
  144.           is allowed,  but no other reading.
  145.  
  146.  
  147. For a scalar parameter, the above effects are achieved  by  copy:   at  the
  148. start  of  each  call, if the mode is in or in out, the value of the actual
  149. parameter is copied into  the  associated  formal  parameter;   then  after
  150. normal completion of the subprogram body, if the mode is in out or out, the
  151. value  of  the  formal  parameter is copied back into the associated actual
  152. parameter.  For a parameter whose type is an access type, copy-in  is  used
  153. for all three modes, and copy-back for the modes in out and out.
  154.  
  155.  
  156. For  a  parameter  whose  type  is  an  array,  record,  or  task  type, an
  157. implementation may likewise achieve the  above  effects  by  copy,  as  for
  158. scalar  types.   In  addition, if copy is used for a parameter of mode out,
  159. then copy-in is required at least for the bounds and discriminants  of  the
  160. actual  parameter  and of its subcomponents, and also for each subcomponent
  161. whose type is an access type.  Alternatively, an implementation may achieve
  162. these effects by reference, that is, by arranging that  every  use  of  the
  163. formal  parameter  (to  read or to update its value) be treated as a use of
  164. the associated actual parameter, throughout the execution of the subprogram
  165. call.  The language does not define which of these two mechanisms is to  be
  166. adopted  for  parameter  passing,  nor  whether different calls to the same
  167. subprogram are to use the same mechanism.  The execution of  a  program  is
  168. erroneous  if  its  effect  depends  on  which mechanism is selected by the
  169. implementation.
  170.  
  171.  
  172. For a parameter whose type  is  a  private  type,  the  above  effects  are
  173. achieved  according to the rule that applies to the corresponding full type
  174. declaration.
  175.  
  176.  
  177. Within the body of a subprogram, a  formal  parameter  is  subject  to  any
  178. constraint   resulting   from   the   type  mark  given  in  its  parameter
  179. specification.  For a formal parameter of an unconstrained array type,  the
  180. bounds  are obtained from the actual parameter, and the formal parameter is
  181. constrained by these bounds (see 3.6.1).   For  a  formal  parameter  whose
  182. declaration  specifies  an  unconstrained  (private  or  record)  type